package com.example.medicalrecord.AggregationModule.administrator.service.impl;
import com.example.medicalrecord.AggregationModule.administrator.mapper.AdministratorMapper;
import com.example.medicalrecord.AggregationModule.administrator.pojo.dto.AdminDoctorAddParam;
import com.example.medicalrecord.AggregationModule.administrator.pojo.dto.AdministratorParam;
import com.example.medicalrecord.AggregationModule.administrator.pojo.dto.AdministratorQuery;
import com.example.medicalrecord.AggregationModule.administrator.pojo.entity.Administrator;
import com.example.medicalrecord.AggregationModule.administrator.pojo.vo.AdministratorVO;
import com.example.medicalrecord.AggregationModule.administrator.pojo.dto.AdminDoctorQuery;
import com.example.medicalrecord.AggregationModule.administrator.pojo.vo.AdminDoctorVO;
import com.example.medicalrecord.AggregationModule.administrator.service.AdministratorService;
import com.example.medicalrecord.AggregationModule.doctor.pojo.entity.Doctor;
import com.example.medicalrecord.base.response.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdministratorServiceImpl implements AdministratorService {
    @Autowired
    AdministratorMapper administratorMapper;

    /*查询管理员数据*/
    @Override
    public PageData selectAdmin(AdministratorQuery administratorQuery) {
        log.debug("查询所有管理员业务:administratorQuery={}",administratorQuery);
        PageData pageData = new PageData();
        List<AdministratorVO> administratorVOList = administratorMapper.selectAdmin(administratorQuery);
        pageData.setList(administratorVOList);
        pageData.setTotal(administratorMapper.selectAdminCount(administratorQuery));
        return pageData;
    }

    /*添加管理员数据*/
    @Override
    public void saveAdmin(AdministratorParam administratorParam) {
        log.debug("添加管理员业务:administratorParam={}",administratorParam);
        Administrator administrator = new Administrator();
        BeanUtils.copyProperties(administratorParam,administrator);
        if (administrator.getId() == null) {
            administrator.setPassword("666666");
            administrator.setStatus("1");
            administrator.setSuperStatus("0");
            administrator.setSole("10");
            administrator.setCreateTime(new Date());
            administratorMapper.insertAdmin(administrator);
        }else {
            administrator.setUpdateTime(new Date());
            administratorMapper.updateAdmin(administrator);
        }

    }
    /*删除管理员数据*/
    @Override
    public void deleteAdmin(Long id) {
        log.debug("删除管理员业务:id={}",id);
        administratorMapper.deleteAdmin(id);
    }
    /*医生数据查询*/
    @Override
    public List<AdminDoctorVO> selectDoctor(AdminDoctorQuery adminDoctorQuery) {
        List<AdminDoctorVO> list = administratorMapper.selectDoctor(adminDoctorQuery);
        log.debug("业务查询医生账号参数={}", list);
        return list;
    }

    /*创建医生账号*/
    @Override
    public void addDoctor(AdminDoctorAddParam adminDoctorAddParam) {
        log.debug("医生创建账号业务数据={}", adminDoctorAddParam);
        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(adminDoctorAddParam, doctor);
        if (doctor.getId()==null){
            /*设置初始值*/
            doctor.setDoctorPassword("123456");
            doctor.setStatus("0");
            doctor.setSole("20");
            doctor.setCreateTime(new Date());
            administratorMapper.insertAdminDoctor(doctor);
        }else {
            doctor.setUpdateTime(new Date());
            administratorMapper.updateStatus(doctor);
        }
    }
    /*修改医生账号的状态 是专家或不是专家*/
    @Override
    public void updateStatus(Long doctorId, String expert) {
        log.debug("修改状态业务层，doctorId={},expert=>{}", doctorId, expert);
        Doctor doctor = new Doctor();
        doctor.setId(doctorId);
        doctor.setExpert(expert);
        doctor.setUpdateTime(new Date());
        administratorMapper.updateStatus(doctor);
    }

    /*医生账号密码重置*/
    @Override
    public void resetPassword(Long doctorId) {
        log.debug("业务重置密码:doctorId={}", doctorId);
        Doctor doctor = new Doctor();
        doctor.setId(doctorId);
        doctor.setDoctorPassword("123456");
        doctor.setUpdateTime(new Date());
        administratorMapper.updateStatus(doctor);
    }

    @Override
    public void deleteDoctor(Long doctorId) {
        log.debug("业务删除医生账号:doctorId=>{}",doctorId);
        administratorMapper.deleteDoctor(doctorId);
    }
}
